#pragma once

#include "../for_hiboost/headers.HiBoost.h"

#include <vector>
#include <string>
#include <math.h>
#include "common.h"
#include "AndNode.h"
#include "LeafNode.h"
#include "AndNodeModel.h"
#include "OrNodeModel.h"

using	namespace	std;

class CAndNode;

class COrNode
{
	public:
		COrNode(void);
		~COrNode(void);

	public:
		int							ModelID;
		vector<int>					children;
		vector<NodeInstanceType>	instances;
		vector<NodeInstanceType>	fullInst;
		vector<int>					fullIndex;
		int							Level;
		int							InstCount;

		//For loopy inference
		vector<int>					CenterIndex;
		vector<int>					ConstrainIndex;
		bool						parseInfo;
		bool						createInfo;

		bool						fNeedMakeInstTree;
		bool						fNeedRefreshMRFScore;


	public:
		bool InputFromStream(istream& inStream);
		void OutputToStream(ostream& outStream) const;

	public:
		int		BottomUpSearch(ModelTreeType &model, CHiBoostImageEntity &entity, InstanceTreeType &tree, const char* ccpPN_ParseLog);
		int		FillInInstInTree(InstanceTreeType &tree, InstanceTreeType &tree_fillin, int InstID);

		void	RefreshMRFScore(ModelTreeType &model, InstanceTreeType& tree);

		int		RemakeInstTree_NoEntity(ModelTreeType &model, InstanceTreeType &tree);
		int		MakeInstTree(ModelTreeType &model, CHiBoostImageEntity &entity, InstanceTreeType &tree, bool fComputeAppearanceScore);

		int		MakeMissLeafNodes(ModelTreeType &model, CHiBoostImageEntity &entity, InstanceTreeType &tree, BasicInstInfoType &center, vector<BasicNodePairType> &LeafNodes);

		double	AnalyseScore(
			vector<double>& DstScoreList_SubTree, vector<double>& DstScoreList_PerNode,
			CHiBoostImageEntity &entity, InstanceTreeType &tree, int InstID
			);
		int		UpdateParameters(
			ModelTreeType &model, ModelTreeType &NewModel,
			CHiBoostImageEntity &entity,
			InstanceTreeType &tree, InstanceTreeType &ground_tree,
			int InstID,
			double UpdatingWeight);

		int		AddSearchCenterIndex(ModelTreeType &model, InstanceTreeType &tree, ArcModelType &Rule, bool NxtStatus);
		int		SetNodeSearchStatus(ModelTreeType &model, InstanceTreeType &tree);

		int		MakeSearchConstrain(ModelTreeType &model, InstanceTreeType &tree, CHiBoostImageEntity &entity);

		// Initialization

	public:
		void	CreateNaiveModelFromNodes(ModelTreeType &model, InstanceTreeType &tree);

};
